package 代码记录.指定公司代码练习记录.西安.分值100分;

import java.util.Scanner;

/**
 * @author zx
 * @create 2023-06-29 22:18
 * 难度大
 */
public class 五子棋迷 {
    /**
     * 自己的代码
     */
    public static void main2(String[] args) {//3
        Scanner sc = new Scanner(System.in);
        int cur = sc.nextInt();
        sc.nextLine();
        String str = sc.nextLine();
        String[] arr = str.split(" ");
        String temp = "";
        for(int i = 0;i < arr.length;i++){
            temp += arr[i];
        }
        char[] num = temp.toCharArray();

    }

    /**
     * 人家的解法：看到一半有点迷
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int target = sc.nextInt();
        sc.nextLine();
        int[] nums = readIntArray(sc.nextLine());
        int resIndex = findMaxMatchIndex(nums, target);
        System.out.println(resIndex);
    }

    // 读取一行字符串并将其转换为整数数组
    private static int[] readIntArray(String line) {
        String[] numsStr = line.split(" ");
        int[] nums = new int[numsStr.length];
        for (int i = 0; i < numsStr.length; i++) {
            nums[i] = Integer.parseInt(numsStr[i]);
        }
        return nums;
    }

    // 查找最大匹配次数的索引
    private static int findMaxMatchIndex(int[] nums, int target) {
        int max = Integer.MIN_VALUE;
        int midDist = 0;
        int resIndex = -1;
        int length = nums.length;
        for (int i = 0; i < length; i++) {
            if (nums[i] == 0) {
                int left = i - 1;
                int right = i + 1;
                int leftCount = 0;
                int rightCount = 0;
                // 计算左侧匹配次数
                while (left >= 0 && nums[left] == target && leftCount < max - 1) {
                    leftCount++;
                    left--;
                }
                if (leftCount > 4) {
                    continue;
                }
                rightCount = leftCount;
                // 计算右侧匹配次数
                while (right < length && nums[right] == target && rightCount < max - 1) {
                    rightCount++;
                    right++;
                }
                // 计算总匹配次数和距离中点的距离
                int totalCount = leftCount + rightCount;
                int distToMid = Math.abs(i - length / 2);

                if (totalCount > max || (totalCount == max && distToMid < midDist)) {
                    max = totalCount;
                    resIndex = i;
                    midDist = distToMid;
                }
            }
        }
        return resIndex;
    }
}
